package com.feng.chat.controller;

import com.feng.chat.dto.Connect;
import com.feng.chat.dto.Result;
import com.feng.chat.entity.Contact;
import com.feng.chat.entity.ContactApply;
import com.feng.chat.service.IContactApplyService;
import com.feng.chat.service.IContactService;
import com.feng.chat.service.ITalkSessionService;
import com.feng.chat.utils.GetUserUtil;
import com.feng.chat.vo.ApplyDeclineVO;
import com.feng.chat.vo.ContactApplyVO;
import com.feng.chat.vo.EditRemarkVO;
import com.feng.chat.vo.TalkCreateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/contact")
public class ContactController {
    @Autowired
    private IContactService contactService;

    @Autowired
    private ITalkSessionService talkSessionService;

    @Autowired
    private IContactApplyService contactApplyService;

    /**
     * 获取未读的申请信息数
     *
     * @param request
     * @return
     */
    @GetMapping("/apply/unread-num")
    public Result applyUnreadNum(HttpServletRequest request) {
        Integer userId = GetUserUtil.getUserId(request);
        Result result = contactService.GetApplyUnreadNum(userId);
        return result;
    }


    /**
     * 获取好友列表
     *
     * @param request
     * @return
     */
    @GetMapping("/list")
    public Result friendList(HttpServletRequest request) {
        Integer userId = GetUserUtil.getUserId(request);
        Result result = contactService.friendList(userId);
        return result;
    }

    /**
     * 通过其手机号查询用户
     */
    @GetMapping("/search")
    public Result search(String mobile) {
        Result result = contactService.search(mobile);
        return result;
    }

    /**
     * 通过id查询用户
     */
    @GetMapping("/detail")
    public Result detail(HttpServletRequest request, Integer user_id) {
        Integer userId = GetUserUtil.getUserId(request);

        Result result = contactService.detail(userId, user_id);
        return result;
    }

    /**
     * 发送好友申请
     *
     * @param contactApplyVO
     * @param request
     * @return
     */
    @PostMapping("/apply/create")
    public Result applyCreate(@RequestBody ContactApplyVO contactApplyVO, HttpServletRequest request) {
        Integer userId = GetUserUtil.getUserId(request);
        ContactApply contactApply = new ContactApply();
        contactApply.setUserId(userId);
        contactApply.setFriendId(contactApplyVO.getFriend_id());
        contactApply.setRemark(contactApplyVO.getRemark());
        Result result = contactService.applyCreate(contactApply);
        return result;
    }


    /**
     * 获取好友申请信息
     *
     * @param request
     * @param page
     * @param page_size
     * @return
     */
    @GetMapping("/apply/records")
    public Result applyRecords(HttpServletRequest request, Integer page, Integer page_size) {
        Integer userId = GetUserUtil.getUserId(request);
        Result result = contactService.applyRecords(userId, page, page_size);
        return result;
    }

    /**
     * 拒绝好友申请
     *
     * @param applyDeclineVO
     * @return
     */
    @PostMapping("/apply/decline")
    public Result applyDecline(@RequestBody ApplyDeclineVO applyDeclineVO) {
        Result result = contactService.applyDecline(applyDeclineVO);
        return result;
    }

    /**
     * 同意好友申请
     * @param applyDeclineVO
     * @return
     */
    @PostMapping("/apply/accept")
    public Result applyAccept(@RequestBody ApplyDeclineVO applyDeclineVO, HttpServletRequest request) {
        Integer userId = GetUserUtil.getUserId(request);
        Result result = contactService.applyAccept(userId, applyDeclineVO);
        if (result.getCode()==200){
            ContactApply contactApply = contactApplyService.getById(applyDeclineVO.getApply_id());
            TalkCreateVO talkCreateVO = new TalkCreateVO();
            talkCreateVO.setReceiver_id(contactApply.getUserId());
            //同意好友申请，新增会话
            talkCreateVO.setTalk_type(1);
            Result result1 = talkSessionService.create(userId, talkCreateVO);
            talkCreateVO.setReceiver_id(userId);
            talkSessionService.create(contactApply.getUserId(),talkCreateVO);
        }
        return result;
    }

    /**
     * 删除好友
     * @param friend_id
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody Map<String,Integer> friend_id, HttpServletRequest request) {
        Integer userId = GetUserUtil.getUserId(request);
        Result result = contactService.delete(friend_id.get("friend_id"), userId);
        return result;
    }

    /**
     * 修改好友备注
     * @param editRemarkVO
     * @param request
     * @return
     */
    @PostMapping("/edit-remark")
    public Result editRemark(@RequestBody EditRemarkVO editRemarkVO,HttpServletRequest request){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = contactService.editRemark(editRemarkVO, userId);
        return result;
    }
}
